package com.winchain.chainbill.protocol.controller;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.feign.PubDictionaryFeign;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.po.PubProtocol;
import com.winchain.chainbill.basic.charge.service.ValComService;
import com.winchain.chainbill.basic.charge.vo.ValComVo;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbDfrl;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbDfrlService;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.chainbill.bill.biz.contract.keyword.EsignDctKeyword;
import com.winchain.chainbill.bill.biz.contract.keyword.EsignSndKeyword;
import com.winchain.chainbill.bill.biz.contract.keyword.EsignXfeKeyword;
import com.winchain.chainbill.bill.biz.discount.vo.BillDct;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.tbill.base.service.TbDctService;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.base.service.TbSndService;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignArtisanKeyword;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignDebitKeyword;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignIntPayKeyword;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignServicePayKeword;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignTbKeyword;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.util.TranslateUtil;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PubProtocolController extends BaseController {
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private PubDictionaryFeign pubDictionaryFeign;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private CbSndService cbSndService;
	@Autowired
	private CbXfeService cbXfeService;
	@Autowired
	private CbDfrlService cbDfrlService;
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private TbDctService tbDctService;
	@Autowired
	private ContService contService;
	@Autowired
	private TbSndService tbSndService;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private ValComService valComService;
	@Autowired
	private CbDctService cbDctService;

	@RequestMapping
	@Logging(module = "协议管理", methods = "获取协议详情")
	public PubProtocol getProtocolDetail(String protoId, boolean convertFlag, String billId) {
		if (convertFlag) {
			return convertProtocolContent(this.pubProtocolFeign.getPubProtocolDetailById(protoId), billId);
		}
		return this.pubProtocolFeign.getPubProtocolDetailById(protoId);
	}

	private PubProtocol convertProtocolContent(PubProtocol pubProtocol, String billId) {
		if (pubProtocol != null) {
			String protoType = pubProtocol.getProtoType();
			String content = convertKnownProperties(protoType, pubProtocol.getContent(), billId);
			List<PubDictionary> dicts = this.pubDictionaryFeign.selectChildDictByParentCode(protoType);
			for (PubDictionary dct : dicts) {
				String replaceText = dct.getExtension1();
				content = content.replace(replaceText, "");
			}
			pubProtocol.setContent(content);
		}
		return pubProtocol;
	}

	private String convertKnownProperties(String protoType, String content, String billId) {
		switch (protoType) {
		case "send_proto":
			return convertSndProto(content, billId);
		case "transfer_proto":
			return convertXfeProto(content, billId);
		case "discount_proto":
			return convertDctProto(content, billId);
		case "artisan_proto":
			return convertArtisanProto(content, billId);
		case "tbdct_proto":
			return convertTbDctProto(content, billId);
		case "intpaypromise_proto":
			return convertTbIntPayProto(content, billId);
		case "service_pay_promise":
			return convertTbServicePayProto(content, billId);
		case "debit_proto":
			return convertDebitProto(content, billId);
		}
		return content;
	}

	private String convertDebitProto(String content, String billId) {
		CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
		String billCode = "";
		String sponsorId = "";
		String finNm = "";
		BigDecimal billAmt = null;
		boolean noData = false;
		ValComVo valComVo = null;
		BigDecimal serFee = null;
		String contType = "18";
		if (rec == null) {
			contType = "54";
			TbRec tbRec = (TbRec) this.tbRecService.selectByKey(billId);
			if (tbRec == null) {
				noData = true;
			} else {
				billCode = tbRec.getBillCode();
				billAmt = tbRec.getBillAmt();
				TbDct dct = this.tbDctService.getDct(billId, "1");
				if (dct == null) {
					sponsorId = CommonDataUtil.getCurEntId();
				} else {
					sponsorId = dct.getBuyerId();
					valComVo = this.valComService.getTbValComVo(dct.getFinId(), tbRec.getGrpId());
					serFee = dct.getSerFee();
					finNm = dct.getFinNm();
				}
			}
		} else {
			billCode = rec.getBillCode();
			billAmt = rec.getBillAmt();
			BillDct dct = this.cbDctService.selectBillDctByBillIdAndState(billId, "1");
			if (dct == null) {
				sponsorId = CommonDataUtil.getCurEntId();
			} else {
				sponsorId = dct.getSellerId();
				valComVo = this.valComService.getTbValComVo(dct.getFinId(), rec.getGrpId());
				serFee = dct.getSerFee();
				finNm = dct.getFinNm();
			}
		}
		EsignDebitKeyword keyword = new EsignDebitKeyword();
		if (!noData) {
			keyword.setDebitBillCode(billCode);
			keyword.setDebitDctAmt("��" + MoneyUtil.format(billAmt));
			keyword.setDebitWitness(finNm);
			if (valComVo != null) {
				StringBuilder sb = new StringBuilder();
				String valType = valComVo.getValType();
				if (valComVo.getValStd() != null) {
					if ("1".equals(valType)) {
						sb.append(valComVo.getValStd().stripTrailingZeros().toPlainString()).append("��");
					} else if ("2".equals(valType)) {
						sb.append(valComVo.getValStd().stripTrailingZeros().toPlainString()).append("%");
					} else if ("3".equals(valType)) {
						sb.append("��").append(valComVo.getValStd().stripTrailingZeros().toPlainString());
					}
				} else if ((sb != null) && (sb.length() > 0)) {
					sb.append("0%");
				}
				if ((sb != null) && (sb.length() > 0)) {
					keyword.setDebitSerFeeRate(sb.toString());
				}
			}
			if (serFee != null) {
				keyword.setDebitSerFee("��" + MoneyUtil.format(serFee));

				ContBas cont = this.contService.getLatestCont(CommonDataUtil.getCurEntId(), billId, contType, null);
				if (cont != null) {
					String signDate = SysDateUtil.parseDate(cont.getContDate(), "yyyyMMdd", "yyyy��MM��dd��");
					keyword.setDebitSponsorSignDate(signDate);
					keyword.setDebitAcceptorSignDate(signDate);
					keyword.setDebitWitnessSignDate(signDate);
					keyword.setDebitContSn(cont.getContSn());
				}
			}
			CusRec sponsor = (CusRec) this.cusRecService.selectByKey(sponsorId);
			keyword.setDebitSponsor(sponsor.getCusName());
			keyword.setDebitSponsorLrNm(sponsor.getLrName());
			keyword.setDebitSponsorPhone(sponsor.getContactorPhone());
			AcctBas txAcct = this.billAcctService.getAcctBas(sponsorId, null, "00");
			keyword.setDebitTxAcctNo(txAcct.getAcctNo());
			keyword.setDebitTxAcctNm(txAcct.getAcctNm());
			keyword.setDebitTxBankNm(txAcct.getFiNm());
		}
		CusRec acceptor = this.cusRecService.getArtisan(null);
		AcctBas cpAcct = this.billAcctService.getAcctBas(acceptor.getCusId(), null, "00");
		keyword.setDebitCpAcctNo(cpAcct.getAcctNo());
		keyword.setDebitCpAcctNm(cpAcct.getAcctNm());
		keyword.setDebitCpBankNm(cpAcct.getFiNm());
		keyword.setDebitAcceptor(acceptor.getCusName());
		keyword.setDebitAcceptorLrNm(acceptor.getLrName());
		keyword.setDebitAcceptorPhone(acceptor.getContactorPhone());
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertDctProto(String content, String billId) {
		CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
		CbRec root = (CbRec) this.cbRecService.selectByKey(rec.getRootId());
		CbSnd snd = this.cbSndService.getBillSndById(root.getBillId());
		CbDct dct = this.cbDctService.selectBillDctByBillIdAndState(billId, "1");
		CusRec discount;
		if (dct == null) {
			discount = (CusRec) this.cusRecService.selectByKey(CommonDataUtil.getCurEntId());
		} else {
			discount = (CusRec) this.cusRecService.selectByKey(dct.getSellerId());
		}
		EsignDctKeyword keyword = new EsignDctKeyword();
		keyword.setDctSellerNm(discount.getCusName());
		keyword.setDctSellerLegalNm(discount.getLrName());
		if (("PERSONAL".equals(discount.getCusType())) || ("SELF_EMPLOYED".equals(discount.getCusType()))) {
			keyword.setDctSellerLegalNm(discount.getContactorName());
		}
		keyword.setDctSellerAddress(getCustAddress(discount));
		keyword.setDctSellerContactPhone(discount.getContactorPhone());

		keyword.setSenderNm(snd.getBuyerNm());

		CusRec buyFinner = (CusRec) this.cusRecService.selectByKey(rec.getFinId());
		keyword.setCreditNm(buyFinner.getCusName());
		keyword.setCreditLegalNm(buyFinner.getLrName());
		keyword.setCreditAddress(getCustAddress(buyFinner));
		keyword.setCreditPhone(buyFinner.getContactorPhone());
		if (dct != null) {
			CusRec finner = (CusRec) this.cusRecService.selectByKey(dct.getFinId());
			keyword.setFinNm(dct.getFinNm());
			keyword.setFinAddress(getCustAddress(finner));
			keyword.setFinLegalNm(finner.getLrName());
			keyword.setFinContactPhone(finner.getContactorPhone());
			keyword.setBillCode(rec.getBillCode());
			keyword.setBillAmt("��" + MoneyUtil.format(rec.getBillAmt()));
			keyword.setDctDays(dct.getDctDays() + "��");
			keyword.setDctAmt("��" + MoneyUtil.format(dct.getDctAmt()));

			BigDecimal dctRate = dct.getFixedRate().add(dct.getVarRate()).add(dct.getEntVarRate())
					.divide(MoneyUtil.HUNDRED, 2, RoundingMode.HALF_UP);
			keyword.setDctRate(dctRate + "%");
			keyword.setDctInt("��" + MoneyUtil.format(dct.getDctInt()));
			keyword.setDctDate(SysDateUtil.parseDate(dct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd"));

			keyword.setBillExpDate(SysDateUtil.parseDate(rec.getExpDate(), "yyyyMMdd", "yyyy-MM-dd"));

			keyword.setActualDctAmt("��" + MoneyUtil.format(dct.getActualAmt()));
			BigDecimal ratio = dct.getDctRatio();
			if (ratio == null) {
				keyword.setDctAmtRate("100%");
			} else {
				keyword.setDctAmtRate(ratio.stripTrailingZeros().toPlainString() + "%");
			}
			ContBas cont = this.contService.getLatestCont(CommonDataUtil.getCurEntId(), billId, "12", null);
			if (cont != null) {
				keyword.setDctContSn(cont.getContSn());
			}
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertXfeProto(String content, String billId) {
		CbXfe xfe = new CbXfe();
		xfe.setBillId(billId);
		xfe.setXfeState("1");
		xfe = (CbXfe) this.cbXfeService.selectOne(xfe);
		if (xfe == null) {
			xfe = new CbXfe();
			xfe.setPrntId(billId);
			xfe.setXfeState("1");
			xfe = (CbXfe) this.cbXfeService.selectOne(xfe);
		}
		boolean showAll = xfe != null;
		String transferId;
		if (showAll) {
			transferId = xfe.getBuyerId();
		} else {
			transferId = CommonDataUtil.getCurEntId();
		}
		CusRec transfer = (CusRec) this.cusRecService.selectByKey(transferId);
		CbRec cbRec = (CbRec) this.cbRecService.selectByKey(billId);
		CbRec root = (CbRec) this.cbRecService.selectByKey(cbRec.getRootId());
		CbSnd snd = this.cbSndService.getBillSndById(root.getBillId());
		CusRec finner = (CusRec) this.cusRecService.selectByKey(cbRec.getFinId());

		EsignXfeKeyword keyword = new EsignXfeKeyword();
		keyword.setTransferNm(transfer.getCusName());
		keyword.setTransferLegalNm(transfer.getLrName());
		if (("PERSONAL".equals(transfer.getCusType())) || ("SELF_EMPLOYED".equals(transfer.getCusType()))) {
			keyword.setTransferLegalNm(transfer.getContactorName());
		}
		keyword.setTransferAddress(getCustAddress(transfer));
		keyword.setTransferContactPhone(transfer.getContactorPhone());

		keyword.setCreditNm(finner.getCusName());
		keyword.setCreditLegalNm(finner.getLrName());
		keyword.setCreditAddress(getCustAddress(finner));
		keyword.setCreditPhone(finner.getContactorPhone());

		keyword.setSellerNm2(snd.getBuyerNm());
		keyword.setBuyerNm(snd.getSellerNm());
		keyword.setRootBillCode(root.getBillCode());
		if (showAll) {
			CusRec seller = (CusRec) this.cusRecService.selectByKey(xfe.getSellerId());
			keyword.setSellerNm(seller.getCusName());
			keyword.setSellerLegalNm(seller.getLrName());
			keyword.setSellerAddress(getCustAddress(seller));
			keyword.setSellerContactPhone(seller.getContactorPhone());
			if (("PERSONAL".equals(seller.getCusType())) || ("SELF_EMPLOYED".equals(seller.getCusType()))) {
				keyword.setSellerLegalNm(seller.getContactorName());
			}
			keyword.setBillAmt("��" + MoneyUtil.format(cbRec.getBillAmt()));
			keyword.setBillCode(cbRec.getBillCode());

			String expDate = cbRec.getExpDate();
			CbDfrl dfrl = new CbDfrl();
			dfrl.setXfeId(xfe.getXfeId());
			dfrl = (CbDfrl) this.cbDfrlService.selectOne(dfrl);
			if (dfrl != null) {
				expDate = dfrl.getExpDate();
			}
			keyword.setBillExpDate(SysDateUtil.parseDate(expDate, "yyyyMMdd", "yyyy-MM-dd"));
			keyword.setRcvDate(SysDateUtil.parseDate(xfe.getXfeDate(), "yyyyMMdd", "yyyy-MM-dd"));
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertSndProto(String content, String billId) {
		boolean showAll = StringUtil.notEmpty(billId);

		CbSnd snd = null;
		String buyerId;
		if (showAll) {
			snd = this.cbSndService.getBillSndById(billId);
			buyerId = snd.getBuyerId();
		} else {
			buyerId = CommonDataUtil.getCurEntId();
		}
		CusRec buyer = (CusRec) this.cusRecService.selectByKey(buyerId);

		EsignSndKeyword keyword = new EsignSndKeyword();
		keyword.setBuyerNm(buyer.getCusName());
		keyword.setBuyerLegalNm(buyer.getLrName());
		keyword.setBuyerAddress(getCustAddress(buyer));
		keyword.setBuyerContactPhone(buyer.getContactorPhone());
		if (showAll) {
			CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
			keyword.setBillAmt("��" + MoneyUtil.format(rec.getBillAmt()));
			keyword.setBillCode(rec.getBillCode());
			keyword.setBillExpDate(SysDateUtil.parseDate(rec.getExpDate(), "yyyyMMdd", "yyyy-MM-dd"));

			CusRec seller = (CusRec) this.cusRecService.selectByKey(snd.getSellerId());
			keyword.setOriginalSellerNm(seller.getCusName());
			keyword.setSellerLegalNm(seller.getLrName());
			if (("PERSONAL".equals(seller.getCusType())) || ("SELF_EMPLOYED".equals(seller.getCusType()))) {
				keyword.setSellerLegalNm(seller.getContactorName());
			}
			keyword.setSellerAddress(getCustAddress(seller));
			keyword.setSellerContactPhone(seller.getContactorPhone());

			CusRec finner = (CusRec) this.cusRecService.selectByKey(rec.getFinId());
			keyword.setCreditNm(finner.getCusName());
			keyword.setCreditLegalNm(finner.getLrName());
			keyword.setCreditAddress(getCustAddress(finner));
			keyword.setCreditPhone(finner.getContactorPhone());
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertArtisanProto(String content, String billId) {
		String cusId = CommonDataUtil.getCurEntId();
		CusRec artisan = this.cusRecService.getArtisan(null);
		CusRec cusRec = (CusRec) this.cusRecService.selectByKey(cusId);
		EsignArtisanKeyword keyword = new EsignArtisanKeyword();
		if (artisan != null) {
			if (!StringUtil.emptyAndNull(billId)) {
				ContBas cont = this.contService.getLatestCont(cusId, billId, "51", null);
				if (cont != null) {
					keyword.setTechContSn(cont.getContSn());
				}
				ValComVo serviceFee = null;
				TbDct tbDct = this.tbDctService.getDct(billId, "1");
				if (tbDct != null) {
					if (!tbDct.getSellerId().equals(tbDct.getBuyerId())) {
						cusRec = (CusRec) this.cusRecService.selectByKey(tbDct.getBuyerId());
					} else {
						cusRec = (CusRec) this.cusRecService.selectByKey(tbDct.getSellerId());
					}
					TbRec tbRec = (TbRec) this.tbRecService.selectByKey(billId);
					keyword.setTecTBillCode(tbRec.getBillCode());
					keyword.setTectDctAmt("��" + MoneyUtil.format(tbDct.getDctAmt()));
					keyword.setTectSerFeeAmt("��" + MoneyUtil.format(tbDct.getSerFee()));
					serviceFee = this.valComService.getTbValComVo(tbDct.getFinId(), cusRec.getGrpId());
				} else {
					CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
					if (rec != null) {
						keyword.setTecTBillCode(rec.getBillCode());
						CbDct dct = this.cbDctService.selectBillDctByBillIdAndState(billId, "1");
						if (dct != null) {
							keyword.setTectDctAmt("��" + MoneyUtil.format(dct.getDctAmt()));
							keyword.setTectSerFeeAmt("��" + MoneyUtil.format(dct.getSerFee()));
							serviceFee = this.valComService.getTbValComVo(dct.getFinId(), cusRec.getGrpId());
						}
					}
				}
				if (serviceFee != null) {
					StringBuilder sb = new StringBuilder();
					String valType = serviceFee.getValType();
					if (serviceFee.getValStd() != null) {
						if ("1".equals(valType)) {
							sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("��");
						} else if ("2".equals(valType)) {
							sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("%");
						} else if ("3".equals(valType)) {
							sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("��");
						}
					} else if ((sb != null) && (sb.length() > 0)) {
						sb.append("0%");
					}
					if ((sb != null) && (sb.length() > 0)) {
						keyword.setTectSerFeeRate(sb.toString());
					}
				}
			}
			keyword.setYfEmail("");
			keyword.setYfPhone(artisan.getContactorPhone());
			keyword.setYfHeader(artisan.getLrName());
			keyword.setYfAddress(getCustAddress(artisan));
			keyword.setYfCompanyName(artisan.getCusName());
			AcctCard acctCard = (AcctCard) this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");
			if (acctCard != null) {
				keyword.setYfAcctNo(acctCard.getAcctNo());
				keyword.setYfBankName(acctCard.getHqFiNm());
				keyword.setYfAcctName(acctCard.getAcctNm());
			}
		}
		keyword.setJfCompanyName(cusRec.getCusName());
		keyword.setJfHeader(cusRec.getLrName());
		keyword.setJfPhone(cusRec.getContactorPhone());
		keyword.setJfEmail("");

		keyword.setTechYearMonthDay(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));

		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertTbDctProto(String content, String billId) {
		String cusId = CommonDataUtil.getCurEntId();
		CusRec rec = (CusRec) this.cusRecService.selectByKey(cusId);
		EsignTbKeyword keyword = new EsignTbKeyword();
		if (!StringUtil.emptyAndNull(billId)) {
			TbDct tbDct = this.tbDctService.getDct(billId, "1");
			ContBas cont = this.contService.getLatestCont(cusId, tbDct.getBillId(), "50", null);
			if (cont != null) {
				keyword.setTbContSn(cont.getContSn());
			}
			CusRec seller = (CusRec) this.cusRecService.selectByKey(tbDct.getSellerId());
			keyword.setTbJfLrName(seller.getLrName());
			keyword.setTbJfCompanyName(seller.getCusName());
			TbRec tbRec = (TbRec) this.tbRecService.selectByKey(billId);
			if (tbRec != null) {
				keyword.settBillAmt(MoneyUtil.number2CNMoney(tbRec.getBillAmt()));
				keyword.setTbSndDate(SysDateUtil.parseDate(tbRec.getSndDate(), "yyyyMMdd", "yyyy��MM��dd��"));
				keyword.setTbExpDate(SysDateUtil.parseDate(tbRec.getExpDate(), "yyyyMMdd", "yyyy��MM��dd��"));
				keyword.setTbRate(genRateStr(tbDct.getVarRate(), tbDct.getFixedRate(), tbDct.getEntVarRate()));
				keyword.settBillCode(tbRec.getBillCode());
				keyword.setTbFocalName(tbDct.getBuyerNm());
				keyword.setTbDate(SysDateUtil.parseDate(tbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"));
				keyword.setTbYfCompanyName(rec.getCusName());
				keyword.setTbYfLrName(rec.getLrName());
				try {
					String billType = TranslateUtil.getConstantName("com.winchain.chainbill.constant.TBillConstants",
							"TbType", tbRec.getBillType());
					keyword.settBillType(billType);
				} catch (Exception e) {
					this.logger.info("����������" + e.getMessage());
				}
			}
		} else {
			keyword.setTbJfCompanyName(rec.getCusName());
			keyword.setTbJfLrName(rec.getLrName());
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertTbIntPayProto(String content, String billId) {
		String cusId = CommonDataUtil.getCurEntId();
		EsignIntPayKeyword keyword = new EsignIntPayKeyword();
		if (!StringUtil.emptyAndNull(billId)) {
			TbDct tbDct = this.tbDctService.getDct(billId, "1");
			CusRec seller = (CusRec) this.cusRecService.selectByKey(tbDct.getSellerId());
			keyword.setApplyCompanyName(seller.getCusName());
			CusRec buyer = (CusRec) this.cusRecService.selectByKey(cusId);
			keyword.setIntPayCompanyNm(buyer.getCusName());
			keyword.setIntPayLrName(buyer.getLrName());
			TbSnd tbSnd = this.tbSndService.selectByBillId(billId);

			keyword.setIntPayAcctNm(tbSnd.getConfirmerNm());
			keyword.setIntPayOpenBank(tbSnd.getConfirmerBankNm());
			keyword.setIntPayAcctNo(tbSnd.getConfirmerAcctNo());
			TbRec tbRec = (TbRec) this.tbRecService.selectByKey(billId);
			if (tbRec != null) {
				keyword.setTbBillCode(tbRec.getBillCode());
				keyword.setTbBillNum("1");
				keyword.setTbBillAmt("��" + MoneyUtil.format(tbRec.getBillAmt()));
				keyword.setActualPayAmt("��" + MoneyUtil.format(tbDct.getDctInt()));
				try {
					String billType = TranslateUtil.getConstantName("com.winchain.chainbill.constant.TBillConstants",
							"TbType", tbRec.getBillType());
					keyword.setIntPayBillType(billType);
				} catch (Exception e) {
					this.logger.info("����������" + e.getMessage());
				}
			}
			keyword.setIntPayDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String convertTbServicePayProto(String content, String billId) {
		String cusId = CommonDataUtil.getCurEntId();
		EsignServicePayKeword keyword = new EsignServicePayKeword();
		if (!StringUtil.emptyAndNull(billId)) {
			TbDct tbDct = this.tbDctService.getDct(billId, "1");
			CusRec seller = (CusRec) this.cusRecService.selectByKey(tbDct.getSellerId());
			keyword.setTbApplyCompany(seller.getCusName());
			TbRec tbRec = (TbRec) this.tbRecService.selectByKey(billId);
			if (tbRec != null) {
				keyword.setSpTBillCode(tbRec.getBillCode());
				keyword.setSpTBillAmt("��" + MoneyUtil.format(tbRec.getBillAmt()));

				keyword.setSpServiceAmt("��" + MoneyUtil.format(tbDct.getSerFee()));
				keyword.setSpTBillDate(SysDateUtil.parseDate(tbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"));
			}
			CusRec cus = (CusRec) this.cusRecService.selectByKey(cusId);
			keyword.setSpCompany(cus.getCusName());
			keyword.setSpLrName(cus.getLrName());
			keyword.setSpSignDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String getCustAddress(CusRec cusRec) {
		StringBuilder builder = new StringBuilder();
		if (StringUtil.notEmpty(cusRec.getProvince())) {
			builder.append(cusRec.getProvince());
		}
		if (StringUtil.notEmpty(cusRec.getCity())) {
			builder.append(cusRec.getCity());
		}
		builder.append(cusRec.getAddress());
		return builder.toString();
	}

	private String genRateStr(BigDecimal varRate, BigDecimal fixedRate, BigDecimal entRate) {
		BigDecimal result = fixedRate;
		if (varRate != null) {
			result = result.add(varRate);
		}
		if (entRate != null) {
			result = result.add(entRate);
		}
		return result.stripTrailingZeros().toPlainString() + " %";
	}
}
